package cn.stylefeng.guns.modular.yingbao.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.yingbao.entity.SysUserMessage;
import cn.stylefeng.guns.modular.yingbao.mapper.SysUserMessageMapper;
import cn.stylefeng.guns.modular.yingbao.model.params.SysUserMessageParam;
import cn.stylefeng.guns.modular.yingbao.model.result.SysUserMessageResult;
import  cn.stylefeng.guns.modular.yingbao.service.SysUserMessageService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 消息存储表 服务实现类
 * </p>
 *
 * @author jj
 * @since 2019-12-20
 */
@Service
public class SysUserMessageServiceImpl extends ServiceImpl<SysUserMessageMapper, SysUserMessage> implements SysUserMessageService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SysUserMessageParam param){
        SysUserMessage entity = getEntity(param);
        this.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(SysUserMessageParam param){
        this.removeById(getKey(param));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysUserMessageParam param){
        SysUserMessage oldEntity = getOldEntity(param);
        SysUserMessage newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public SysUserMessageResult findBySpec(SysUserMessageParam param){
        return null;
    }

    @Override
    public List<SysUserMessageResult> findListBySpec(SysUserMessageParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(SysUserMessageParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public ResponseData appList(Long userid) {

        List<SysUserMessage> list = baseMapper.selectList(new QueryWrapper<SysUserMessage>().eq("userid", userid).eq("stuats",0));
        if (list.size()<1){
            return ResponseData.error("暂无数据");
        }
        for (SysUserMessage sysUserMessage:list){
            sysUserMessage.setStuats(2);
        }
        boolean b = this.updateBatchById(list);
        if (!b){
            return ResponseData.error("处理错误！");
        }
        return ResponseData.success(200,"成功！",list);
    }

    @Override
    public void deleteDate() {
        Date today = new Date();
        //获取三十天前日期
        Calendar theCa = Calendar.getInstance();
        theCa.setTime(today);
        theCa.add(theCa.DATE, -7);//最后一个数字30可改，30天的意思
        Date start = theCa.getTime();
        int create_date = baseMapper.delete(new QueryWrapper<SysUserMessage>().lt("create_time", start));
        if (create_date>0){
            System.out.println("成功");
        }

    }

    private Serializable getKey(SysUserMessageParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private SysUserMessage getOldEntity(SysUserMessageParam param) {
        return this.getById(getKey(param));
    }

    private SysUserMessage getEntity(SysUserMessageParam param) {
        SysUserMessage entity = new SysUserMessage();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
